| Conditions | 1 |
| Paths | 384 |
| Total Lines | 214 |
| Code Lines | 159 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var TableHelperRequestSort = function(rq, strDesc){ |
||
| 145 | function initInstance(){ |
||
| 146 | // Singleton |
||
| 147 | // Private methods and variables |
||
| 148 | function BuildRequest(rq, crntTableId){ |
||
| 149 | rq.tableId = crntTableId; |
||
| 150 | TableHelperRequestSort(rq, instance.strDesc); |
||
| 151 | TableHelperRequestFilter(rq); |
||
| 152 | } |
||
| 153 | function getParent(obj, objType){ |
||
| 154 | while(obj && obj.tagName !== objType.toUpperCase()){ |
||
| 155 | obj = obj.parentNode; |
||
| 156 | } |
||
| 157 | return obj; |
||
| 158 | } |
||
| 159 | function FilterGetTableId(field){ |
||
| 160 | if(field.tagName.toLowerCase() !== "select"){ |
||
| 161 | return field.getAttribute("data-table-id"); |
||
| 162 | } |
||
| 163 | var f = field.parentNode.parentNode.getElementsByTagName("input")[0]; |
||
| 164 | return '' === f.value ? null : f.getAttribute("data-table-id"); |
||
| 165 | } |
||
| 166 | var tail = []; |
||
| 167 | function LoadData(tableContainer, rq){ |
||
| 168 | SetVisability(tableContainer, false); |
||
| 169 | if(window.XMLHttpRequest){ |
||
| 170 | var xmlhttp = new XMLHttpRequest();/* code for IE7+, Firefox, Chrome, Opera, Safari */ |
||
| 171 | }else{ |
||
| 172 | xmlhttp = new window.ActiveXObject("Microsoft.XMLHTTP");/*code for IE6, IE5 */ |
||
| 173 | } |
||
| 174 | for(var i = 0; i < tail.length; i++){ |
||
| 175 | var ex_xmlhttp = tail.shift(); |
||
| 176 | ex_xmlhttp.abort(); |
||
| 177 | } |
||
| 178 | xmlhttp.onreadystatechange = function(){ |
||
| 179 | if(xmlhttp.readyState === 4 && xmlhttp.status === 200){ |
||
| 180 | var d = JSON.parse(xmlhttp.responseText); |
||
| 181 | instance.rq = rq; |
||
| 182 | instance.Draw(tableContainer, d); |
||
| 183 | } |
||
| 184 | }; |
||
| 185 | xmlhttp.open("GET", RequestToUrl(rq), true); |
||
| 186 | xmlhttp.send(); |
||
| 187 | tail.push(xmlhttp); //put at tail to can abort later any previous |
||
| 188 | } |
||
| 189 | function clearSection(tSection){ |
||
| 190 | if(iePrior(9)){ |
||
| 191 | if(tSection.firstChild){ |
||
| 192 | while(tSection.firstChild){ |
||
| 193 | tSection.removeChild(tSection.firstChild); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | }else{ |
||
| 197 | tSection.innerHTML = ""; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | function SetTheTableColumnsHoverEffect(tableContainer){ |
||
| 201 | if(iePrior(9)){ |
||
| 202 | return; |
||
| 203 | } |
||
| 204 | var tContainer = document.getElementById(tableContainer); |
||
| 205 | var tHcells = tContainer.rows[0].cells; |
||
| 206 | for(var i = 0; i < tHcells.length; i++){ |
||
| 207 | if(tHcells[i].firstChild.tagName === "A"){ |
||
| 208 | tHcells[i].firstChild.setAttribute("onmouseover", "table.ColumnHover('" + tableContainer + "'," + i + ");"); |
||
| 209 | tHcells[i].firstChild.setAttribute("onmouseout", "table.ColumnHover('" + tableContainer + "');"); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | var tfoot = tContainer.getElementsByTagName("tfoot")[0]; |
||
| 213 | ProcessPaginationLinks(tfoot); |
||
| 214 | } |
||
| 215 | var iePrior = TableHelperIfPrior; |
||
| 216 | var ProcessPaginationLinks = TableHelperProcessPaginationLinks; |
||
| 217 | var SetVisability = TableHelperSetVisability; |
||
| 218 | var RequestToUrl = TableHelperRequestToUrl; |
||
| 219 | |||
| 220 | return { |
||
| 221 | rq: null, |
||
| 222 | strAsc: String.fromCharCode(9650), //▲ |
||
| 223 | strDesc: String.fromCharCode(9660),//▼ |
||
| 224 | ReloadData: function(tableId){ |
||
| 225 | var request = {}; |
||
| 226 | BuildRequest(request, tableId); |
||
| 227 | LoadData(tableId, request); |
||
| 228 | }, |
||
| 229 | Filter: function(field){ |
||
| 230 | var crntTableId = FilterGetTableId(field); |
||
| 231 | if(crntTableId !== null){ |
||
| 232 | var request = {}; |
||
| 233 | var exRq = this.rq; |
||
| 234 | BuildRequest(request, crntTableId); |
||
| 235 | if(exRq === null || |
||
| 236 | request.filter !== exRq.filter || |
||
| 237 | request.filterBy !== exRq.filterBy |
||
| 238 | ){ |
||
| 239 | LoadData(crntTableId, request); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | }, |
||
| 243 | GoPage: function(lnk){ |
||
| 244 | var request = {}; |
||
| 245 | var table = getParent(lnk, "table"); |
||
| 246 | var crntTableId = table.getAttribute("id"); |
||
| 247 | BuildRequest(request, crntTableId); |
||
| 248 | //check & serve pagination jump links |
||
| 249 | var jumpDir = lnk.innerHTML.trim().substr(0, 1); |
||
| 250 | if(jumpDir === "+" || jumpDir === "-"){ |
||
| 251 | var current = table.querySelector("tfoot .paging .a").innerHTML; |
||
| 252 | var jump = lnk.innerHTML.replace("K", "000").replace("M", "000000000"); |
||
| 253 | var jumpPage = (parseInt(current) + parseInt(jump)); |
||
| 254 | lnk.parentNode.setAttribute("data-page", jumpPage); |
||
| 255 | lnk.style.transform = "none"; |
||
| 256 | } |
||
| 257 | request.pageNo = lnk.parentNode.hasAttribute("data-page") ? |
||
| 258 | lnk.parentNode.getAttribute("data-page") : |
||
| 259 | lnk.innerHTML; |
||
| 260 | LoadData(crntTableId, request); |
||
| 261 | return false; |
||
| 262 | }, |
||
| 263 | Export: function(lnk, eType){ |
||
| 264 | var request = {}; |
||
| 265 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 266 | BuildRequest(request, crntTableId); |
||
| 267 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ? |
||
| 268 | eType : |
||
| 269 | "csv"; |
||
| 270 | window.open(RequestToUrl(request)); |
||
| 271 | return false; |
||
| 272 | }, |
||
| 273 | Sort: function(colNo, lnk){ |
||
| 274 | var request = {}; |
||
| 275 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 276 | BuildRequest(request, crntTableId); |
||
| 277 | if(Math.round(colNo) === request.colNo){ |
||
| 278 | request.colOrd = (request.colOrd === "asc" ? "desc" : "asc"); |
||
| 279 | }else{ |
||
| 280 | request.colNo = Math.round(colNo); |
||
| 281 | request.colOrd = "asc"; |
||
| 282 | } |
||
| 283 | LoadData(crntTableId, request); |
||
| 284 | /* Clear and add new sort arrow */ |
||
| 285 | var headSpans = getParent(lnk, "thead").getElementsByTagName("span"); |
||
| 286 | var length = headSpans.length; |
||
| 287 | for(var i = 0; i < length; i++){ |
||
| 288 | headSpans[i].innerHTML = ""; |
||
| 289 | } |
||
| 290 | lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? this.strDesc : this.strAsc); |
||
| 291 | }, |
||
| 292 | DrawSection: function(tableContainer, dt, tSection){ |
||
| 293 | var section = tSection === "tfoot" ? "tfoot" : "tbody"; |
||
| 294 | tSection = document.getElementById(tableContainer). |
||
| 295 | getElementsByTagName(section)[0]; |
||
| 296 | clearSection(tSection); |
||
| 297 | for(var i = 0; i < dt.length; i++){ |
||
| 298 | var row = dt[i]; |
||
| 299 | var tRow = document.createElement("tr"); |
||
| 300 | |||
| 301 | this.DrawRow(row, tRow); |
||
| 302 | |||
| 303 | tSection.appendChild(tRow); |
||
| 304 | if(section === "tfoot"){ |
||
| 305 | ProcessPaginationLinks(tSection); |
||
| 306 | } |
||
| 307 | this.AppendRowCalback(tableContainer); |
||
| 308 | } |
||
| 309 | }, |
||
| 310 | DrawRow: function(row, tRow){ |
||
| 311 | for(var cell in row){ |
||
|
1 ignored issue
–
show
|
|||
| 312 | var tCell = document.createElement("td"); |
||
| 313 | if(typeof row[cell] === "string" || typeof row[cell] === "number"){ |
||
| 314 | tCell.innerHTML = row[cell]; |
||
| 315 | }else if(typeof row[cell] === "object"){ |
||
| 316 | this.DrawCellFromObject(row, cell, tCell); |
||
| 317 | } |
||
| 318 | tRow.appendChild(tCell); |
||
| 319 | } |
||
| 320 | }, |
||
| 321 | DrawCellFromObject: function(row, cell, tCell){ |
||
| 322 | for(var attr in row[cell]){ |
||
|
1 ignored issue
–
show
|
|||
| 323 | if(typeof row[cell][attr] === "string"){ |
||
| 324 | tCell.innerHTML = row[cell][attr]; |
||
| 325 | }else if(typeof row[cell][attr] === "object"){ |
||
| 326 | for(var v in row[cell][attr]){ |
||
|
1 ignored issue
–
show
|
|||
| 327 | tCell.setAttribute(v, row[cell][attr][v]); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | } |
||
| 331 | }, |
||
| 332 | |||
| 333 | ColumnHover: function(tableContainer, index){ |
||
| 334 | if(!iePrior(9)){ |
||
| 335 | TableHelperColumnHover.call(this, tableContainer, index); |
||
| 336 | } |
||
| 337 | }, |
||
| 338 | Draw: function(tableContainer, d){ |
||
| 339 | this.DrawSection(tableContainer, d.body); |
||
| 340 | this.DrawSection(tableContainer, d.footer, "tfoot"); |
||
| 341 | this.LoadEndCalback(tableContainer); |
||
| 342 | SetVisability(tableContainer, true); |
||
| 343 | if(this.rq !== null){ |
||
| 344 | var hover = document.getElementById(this.rq.tableId) |
||
| 345 | .getElementsByTagName("th")[this.rq.colNo].lang; |
||
| 346 | if(hover){ |
||
| 347 | this.ColumnHover(tableContainer, this.rq.colNo); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | }, |
||
| 352 | init: function(tableId){ |
||
| 353 | SetTheTableColumnsHoverEffect(tableId); |
||
| 354 | }, |
||
| 355 | LoadEndCalback: function(){},/*Allows override: function(tableId){if(tableId){...}}*/ |
||
| 356 | AppendRowCalback: function(){}/*Allows override: function(tableId){if(tableId){...}}*/ |
||
| 357 | }; |
||
| 358 | } |
||
| 359 | return { |
||
| 370 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: